import request from './config'
import { showToast } from 'vant'

// 分类类型定义
export interface Category {
  id: string;
  categoryName: string;
  description?: string;
  productCount?: number;
  createTime?: string;
  updateTime?: string;
}

// 查询参数
export interface CategoryQuery {
  categoryName?: string
  current?: number
  size?: number
}

// 分页结果
export interface PageResult<T> {
  records: T[]
  total: number
  size: number
  current: number
  pages: number
}

// 获取分类列表
export const getCategoryList = async () => {
  try {
    const response = await request.get('/category/list')
    
    // 假设后端返回结构：{ code, msg, data }
    if (response?.data?.code === 200 && response.data.data) {
      return response.data.data.map((item: any) => ({
        id: item.id,
        categoryName: item.categoryName,
        description: item.description,
        productCount: item.productCount || 0,
        createTime: item.createTime,
        updateTime: item.updateTime
      })) as Category[]
    }
    
    console.error('获取分类列表失败:', response?.data?.msg || '未知错误')
    showToast(response?.data?.msg || '获取分类列表失败')
    return []
  } catch (error) {
    console.error('获取分类列表失败:', error)
    showToast('获取分类列表失败')
    return []
  }
}

/**
 * 获取分类分页列表
 * @param query 查询参数
 */
export const getCategoryPage = async (query: CategoryQuery): Promise<PageResult<Category>> => {
  try {
    const { data } = await request({
      url: '/category/pageCategories',
      method: 'POST',
      data: query
    })
    
    if (data?.code === 200 && data.data) {
      return data.data
    }
    
    console.error('获取分类分页数据失败:', data?.msg || '未知错误')
    showToast(data?.msg || '获取分类数据失败')
    return {
      records: [],
      total: 0,
      size: 10,
      current: 1,
      pages: 0
    }
  } catch (error) {
    console.error('获取分类分页数据失败:', error)
    showToast('获取分类数据失败')
    return {
      records: [],
      total: 0,
      size: 10,
      current: 1,
      pages: 0
    }
  }
}

/**
 * 获取分类详情
 * @param id 分类ID
 */
export const getCategoryDetail = async (id: string): Promise<Category | null> => {
  try {
    const { data } = await request({
      url: `/category/${id}`,
      method: 'GET'
    })
    return data
  } catch (error) {
    console.error('获取分类详情失败:', error)
    showToast('获取分类详情失败')
    return null
  }
}

// 添加新分类
export const addCategory = async (category: Omit<Category, 'id'>) => {
  try {
    const response = await request.post('/category/addCategory', {
      categoryName: category.categoryName,
      description: category.description || ''
    })
    
    if (response?.data?.code === 200) {
      showToast('添加分类成功')
      return true
    }
    
    console.error('添加分类失败:', response?.data?.msg || '未知错误')
    showToast(response?.data?.msg || '添加分类失败')
    return false
  } catch (error) {
    console.error('添加分类失败:', error)
    showToast('添加分类失败')
    return false
  }
}

// 更新分类
export const updateCategory = async (category: Category) => {
  try {
    const response = await request.post('/category/updateCategory', {
      id: category.id,
      categoryName: category.categoryName,
      description: category.description || ''
    })
    
    if (response?.data?.code === 200) {
      showToast('更新分类成功')
      return true
    }
    
    console.error('更新分类失败:', response?.data?.msg || '未知错误')
    showToast(response?.data?.msg || '更新分类失败')
    return false
  } catch (error) {
    console.error('更新分类失败:', error)
    showToast('更新分类失败')
    return false
  }
}

// 删除分类
export const deleteCategory = async (id: string) => {
  try {
    const response = await request.post('/category/deleteCategory', null, {
      params: { id }
    })
    
    if (response?.data?.code === 200) {
      showToast('删除分类成功')
      return true
    }
    
    console.error('删除分类失败:', response?.data?.msg || '未知错误')
    showToast(response?.data?.msg || '删除分类失败')
    return false
  } catch (error) {
    console.error('删除分类失败:', error)
    showToast('删除分类失败')
    return false
  }
} 